<script setup name="DocMyView">
import {useRoute} from "vue-router"
import {getDocStatusStyle} from "@/utils/document.js";
import {getButtons, getDocMyBasicInfo} from "@/api/document/myDocument.js";
import {getDocumentOperateMenu} from "@/utils/menu.js";
import Steps from "@/views/common/steps.vue";
import DocumentUpdate from "@/views/document/my/basicInfo/documentUpdate.vue";
import DocumentView from "@/views/document/common/basicInfo/documentView.vue";
import DraftView from "@/views/document/my/flowInfo/draft/draftView.vue";
import AuditView from "@/views/document/my/flowInfo/audit/auditView.vue";
import JointSignView from "@/views/document/my/flowInfo/jointSign/jointSignView.vue";
import LeaderSignView from "@/views/document/my/flowInfo/leaderSign/leaderSignView.vue";
import AttachmentList from "@/views/common/attachment/attachmentList.vue";
import SignView from "@/views/document/my/flowInfo/sign/signView.vue";
import CancelView from "@/views/document/my/flowInfo/cancel/cancelView.vue";
import RedSheetView from "@/views/document/my/flowInfo/redSheet/redSheetView.vue";
import DistributeView from "@/views/document/my/flowInfo/distribute/distributeView.vue";
import CompleteView from "@/views/document/my/flowInfo/complete/completeView.vue";
import ArchiveView from "@/views/document/my/flowInfo/archive/archiveView.vue";
import {copyJsonPropertyValue} from "@/utils/ruoyi.js";
import {deleteDocMyAttachment, getDocMyAttachmentFile, getDocMyAttachmentPageList} from "@/api/document/attachment.js";
import {tabFunctions} from "@/utils/tab.js";

const {proxy} = getCurrentInstance()
const {docStatus} = proxy.useDict(["docStatus", false])
const activeTabName = ref('advice')
const route = useRoute()
const docId = route.params && route.params.docId
const status = ref('')
const documentInfo = ref({})
const opMenuEnum = getDocumentOperateMenu()
provide('currentMenu', opMenuEnum.my)
const data = reactive({
      flowInfoButtons: {
        isEdit: false,
        isDelete: false,
        isCancel: false,
        isCompareClick: false,
        isApply: false,
        isNext: false,
        isOpenFinalPdf: false,
      },
      basicInfoButtons: {
        isEdit: false
      },
      attachmentButtons: {
        isAddAttachment: false,
        isDeleteAttachment: false,
      },
      attachmentOpInfo: {
        listQueryParams: {
          docId: docId
        },
        backListUrl: '/document/' + opMenuEnum.my,
        addUrl:  '/doc/myDocAttachment/add',
        openUrl:  '/document/attachment/openFile/',
        deleteApi: deleteDocMyAttachment,
        getFileApi: getDocMyAttachmentFile,
        getListApi: getDocMyAttachmentPageList
      },
      isRefreshAttachment: false,
      isRefreshAdvice: false,
  }
)
const {flowInfoButtons, basicInfoButtons, attachmentButtons, attachmentOpInfo, isRefreshAttachment, isRefreshAdvice} = toRefs(data)
provide('isRefreshAdvice', isRefreshAdvice.value)
const {handChangeTab} = tabFunctions(activeTabName)

onMounted(() => {
  loadMainViewData()
})

function loadMainViewData() {
  if (docId) {
    getDocBasicInfo()
    getButtonsInfo()
  }
}

function getDocBasicInfo() {
  getDocMyBasicInfo(docId).then(res => {
    documentInfo.value = res
    status.value = res.status
  })
}

function getButtonsInfo() {
  getButtons(docId).then(res => {
    flowInfoButtons.value = copyJsonPropertyValue(flowInfoButtons.value, res)
    basicInfoButtons.value = copyJsonPropertyValue(basicInfoButtons.value, res)
    attachmentButtons.value = copyJsonPropertyValue(attachmentButtons.value, res)
  })
}

const handClickTab = (tab, event) => {
  //实时刷新页面数据
  loadMainViewData()
  if ('advice' === tab.props.name) {
    isRefreshAdvice.value = true
  } else if ('attachment' === tab.props.name) {
    isRefreshAttachment.value = true
  }
  setTimeout(() => {
    setRefreshData()
  },50)
}

const setRefreshData = () => {
  isRefreshAttachment.value = false
  isRefreshAdvice.value = false
}

</script>

<template>
  <div class="app-container">
    <el-card :span="24" class="mb10">
      <Steps :status="status" :statusSteps="docStatus.filter(item => (item.value !== '已废弃'))"/>
    </el-card>
    <el-card :span="24">
      <template #header>
        <el-row style="line-height: 40px">
          <el-text type="primary" style="font-size: 18px; ">{{documentInfo.subject}}</el-text>
          <el-text class="ml10">
            <el-tag :style="getDocStatusStyle(documentInfo.status)">状态：{{documentInfo.status}}</el-tag>
            <el-tag :style="getDocStatusStyle(documentInfo.status)" class="ml10">当前办理人：{{documentInfo.operatorName}}</el-tag>
            <el-tag type="info" effect="plain" class="ml10">拟稿人：{{documentInfo.userName}}</el-tag>
            <el-tag type="info" effect="plain" class="ml10">拟稿时间：{{documentInfo.createTime}}</el-tag>
            <el-tag type="info" effect="plain" class="ml10" v-if="documentInfo.updateTime!==null">更新时间：{{documentInfo.updateTime}}</el-tag>
          </el-text>
        </el-row>
      </template>
      <el-row>
        <div class="el-table el-table--enable-row-hover el-table--medium">
          <el-tabs v-model="activeTabName" @tab-change="handChangeTab" @tab-click="handClickTab">
            <el-tab-pane label="流程信息" name="advice" lazy>
              <DraftView :docId="documentInfo.id" :flowInfoButtons="flowInfoButtons" :isRefreshAdvice="isRefreshAdvice" v-if="status==='拟稿'"
                         @loadMainViewData="loadMainViewData"/>
              <AuditView :docId="documentInfo.id" :flowInfoButtons="flowInfoButtons" :isRefreshAdvice="isRefreshAdvice" v-if="documentInfo.status==='审核修订'"
                         @loadMainViewData="loadMainViewData"/>
              <JointSignView :docId="documentInfo.id" :flowInfoButtons="flowInfoButtons" :isRefreshAdvice="isRefreshAdvice" v-if="documentInfo.status==='会签'"
                        @loadMainViewData="loadMainViewData"/>
              <LeaderSignView :docId="documentInfo.id" :flowInfoButtons="flowInfoButtons" :isRefreshAdvice="isRefreshAdvice" v-if="documentInfo.status==='领导签发'"
                        @loadMainViewData="loadMainViewData"/>
              <RedSheetView :docId="documentInfo.id" :revisionCount="documentInfo.mainRevisionCount" :flowInfoButtons="flowInfoButtons" :isRefreshAdvice="isRefreshAdvice"
                            v-if="documentInfo.status==='清稿套红'" @loadMainViewData="loadMainViewData"/>
              <SignView :docId="documentInfo.id" :flowInfoButtons="flowInfoButtons" :isRefreshAdvice="isRefreshAdvice" v-if="documentInfo.status==='签章'"
                        @loadMainViewData="loadMainViewData"/>
              <DistributeView :docId="documentInfo.id" :flowInfoButtons="flowInfoButtons" :isRefreshAdvice="isRefreshAdvice" v-if="documentInfo.status==='分发'"
                        @loadMainViewData="loadMainViewData"/>
              <CompleteView :docId="documentInfo.id" :flowInfoButtons="flowInfoButtons" :isRefreshAdvice="isRefreshAdvice" v-if="documentInfo.status==='办结'"
                        @loadMainViewData="loadMainViewData"/>
              <ArchiveView :docId="documentInfo.id" :isRefreshAdvice="isRefreshAdvice" v-if="documentInfo.status==='归档'" />
              <CancelView :docId="documentInfo.id" :isOpenFinalPdf="flowInfoButtons.isOpenFinalPdf" :isRefreshAdvice="isRefreshAdvice" v-if="documentInfo.status==='已废弃'"/>
            </el-tab-pane>
            <el-tab-pane label="基本信息" name="documentBasicInfo" lazy>
              <DocumentView :documentInfo="documentInfo" v-if="!basicInfoButtons.isEdit"/>
              <DocumentUpdate :documentInfo="documentInfo" v-if="basicInfoButtons.isEdit" @getDocBasicInfo="getDocBasicInfo" />
            </el-tab-pane>
            <el-tab-pane label="公文附件" name="attachment" lazy>
              <AttachmentList :attachmentButtons="attachmentButtons" :attachmentOpInfo="attachmentOpInfo" :isRefreshAttachment="isRefreshAttachment"/>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-row>
    </el-card>
  </div>
</template>
